iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 18
0
AI & Data

DialogFlow從零開始30天系列 第 18

DialogFlow從零開始18天-Webhook 佈署到Cloud Run

  • 分享至 

  • xImage
  •  

前言:

   程式寫好,本機測試ok,那就可以發佈到雲端主機 Cloud Run 啦, GoGo。

打開Cloud Shell

啟用Cloud Run API

gcloud services enable run.googleapis.com	

shell

創建一個名為的新目錄 ”project_name”-nodejs ,並切換到該目錄

mkdir project_name-nodejs
cd project_name-nodejs

使用 vi 創建一個package.json文件

{
  "name": "project_name-nodejs",
  "version": "1.0.0",
  "description": "專案介紹",
  "main": "index.js",
  "scripts": {
    "start": "node index.js"
  },
  "author": "",
  "license": "Apache-2.0",
  "dependencies": {
    "actions-on-google": "^2.12.0",
    "axios": "^0.19.0",
    "body-parser": "^1.19.0",
    "dialogflow": "^0.10.3",
    "express": "^4.17.1",
    "request": "^2.88.0",
    "superagent": "^5.1.0"
  }
}

同一目錄中,創建一個 index.js ,貼上你的node.js寫好的 code

const express = require('express');
const app = express();

app.get('/', (req, res) => {
  console.log('Hello world received a request.');

  const target = process.env.TARGET || 'World';
  res.send(`Hello ${target}!`);
});

const port = process.env.PORT || 8080;
app.listen(port, () => {
  console.log('Hello world listening on port', port);
});

同一目錄中,創建一個 Dockerfile

# Use the official Node.js 10 image.
# https://hub.docker.com/_/node
FROM node:10

# Create and change to the app directory.
WORKDIR /usr/src/app

# Copy application dependency manifests to the container image.
# A wildcard is used to ensure both package.json AND package-lock.json are copied.
# Copying this separately prevents re-running npm install on every code change.
COPY package*.json ./

# Install production dependencies.
RUN npm install --only=production

# Copy local code to the container image.
COPY . .

# Run the web service on container startup.
CMD [ "npm", "start" ]

使用Cloud Build構建容器映像

gcloud config get-value project_name-nodejs

gcloud builds submit --tag gcr.io/project_name-nodejs/project_name

gcloud container images list

docker run -d -p 8080:8080 gcr.io/project_name-nodejs/project_name

將容器化的應用程序部署到Cloud Run中

gcloud beta run deploy --image gcr.io/project_name-nodejs/project_name

成功後,命令行將顯示服務URL:

//On success, the command line displays the service URL:        
https://project_name-ssirjtnadaxx-an.a.run.app/

恭喜你!剛剛將打包在容器映像中的應用程序部署到了Cloud Run。
在把服務URL貼回到fulfillment,就完成啦
fulfillment

使用心得:

     使用雲端server 選用 cloud run 是不是很簡單,再來就是把AoG上架。 

  

https://google.qwiklabs.com/focuses/5162?parent=catalog


上一篇
DialogFlow從零開始17天-Webhook 本機測試工具-ngrok
下一篇
DialogFlow從零開始19天-上架Actions on Google上架準備
系列文
DialogFlow從零開始30天30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言